import data from '../common/data.json';
//详情页面
;(function(){
    //详情页需要的东西goods_id,imgsrc.title
    var GLOBAL_GOODS = [];
    data.goods_list.forEach((item,index)=>{
        GLOBAL_GOODS.push({
            id:item.goods_id,
            img : item.thumb_url,
            title : item.short_name,
            price : parseInt(item.group_price/100)
        })
    })

    
    //渲染详情页
    function renderDetailPage(){
        var deDiv = $(".detail_con");
        var deId = {};
        var localId = localStorage.getItem("dataId");
        GLOBAL_GOODS.forEach((item,index)=>{
            if(item.id ==localId){
                deId = {
                    id : item.id,
                    img : item.img,
                    title : item.title,
                    price : item.price
                }
            }
        })
        deDiv.html(`
        <div class="img" data-id="${deId.id}">
        <a href="javascript:void(0);"  class="waitload to_detail">
            <img width="162" height="162" src="${deId.img}">
        </a>
        </div>
        <h3 class="title to_detailh">
        <a href="#">${deId.title}</a>
        </h3>
        <p class="pri">￥<span>${deId.price}</span></p>
         <div class= "cartBtn">
            <span class= "reduceBtn">-</span>
            <span class= "add_total">0</span>
        </div>
        <span class="inquiry_a open addcart" >加入购物车</span>
        `);
    }

    //购物车开始
    var addCart = $.Callbacks();
    var reduceCart = $.Callbacks();
    function init(){
        renderDetailPage();
        renderCartsNum();
        renderFristPageNum();
        bindEvent();
    }
    function renderFristPageNum(){
        //从GLOBAL_CARTS中拿到id和当前页面的data-id相对比，匹配就拿出count
        var nowCount = 0;
        var nowid = $(".img").attr("data-id");
        var hasCount=GLOBAL_CARTS.some((item,index)=>{
            if(item.id == nowid){
                nowCount = item.count;
                return true;
            }
        })
        if(hasCount){
            $(".addcart").css({
                width:"30px"
            })
            $(".addcart").html("+");
            $(".cartBtn").css({"display":"inline-block"});
            $(".add_total").html(nowCount);
        }
    }
    function changeSelfStyle(){
        $(this).css({
            width:"30px"
        })
        $(this).html("+");
        $(this).siblings(".cartBtn").css({"display":"inline-block"});
    }
    var GLOBAL_CARTS = getCarts();
    function getCarts(){
        if(localStorage.getItem("carts")){
            return JSON.parse(localStorage.getItem("carts"));
        }else{
            return [];
        }
    }
    function addCartsData(){
        var nowid = $(this).siblings(".img").attr("data-id");
        var price = $(this).siblings(".pri").children("span").html();
        var img = $(this).siblings(".img").children(".to_detail").children("img").attr("src");
        
        var hasSameGoods = GLOBAL_CARTS.some(item=>{
            if(item.id ==nowid){
                item.count ++;
                return true;
            }
        })
        if(!hasSameGoods){
            GLOBAL_CARTS.push({
                id : nowid,
                count:1,
                price : price,
                img : img
            })
        }
        saveData("carts",GLOBAL_CARTS);
        GLOBAL_CARTS.some(item=>{
            if(item.id == nowid){
                $(this).siblings(".cartBtn").children(".add_total").html(item.count);
                return true;
            }
        })
    }
    function reduceCartsData(){
        var nowid = $(this).parent().siblings(".img").attr("data-id");
        GLOBAL_CARTS.some((item,index)=>{
            if(item.id == nowid){
                item.count --;
                item.count == 0 ? removeCartsData(nowid,index) : "";
                return true;
            }
        })
        saveData("carts",GLOBAL_CARTS);
        GLOBAL_CARTS.some(item=>{
            if(item.id == nowid){
                $(this).parent(".cartBtn").children(".add_total").html(item.count);
                return true;
            }
        })
    }
    function removeCartsData(id,index){
        $(`.img[data-id=${id}]`).siblings(".cartBtn").css({"display":"none"});
        $(`.img[data-id=${id}]`).siblings(".addcart").css({
            width:"100px"
        });
        $(`.img[data-id=${id}]`).siblings(".addcart").html("加入购物车");
        GLOBAL_CARTS.splice(index,1);
    }
    function saveData(type,json){
        localStorage.setItem(type,JSON.stringify(json));
    }
    //购物车里的数量是商品的总数量
    function renderCartsNum(){
        var total = 0;
        GLOBAL_CARTS.forEach((item,index)=>{
            total +=item.count;
        })
        $(".cartDiv").children().html(total);
    }
    function toCartPage(){
        window.open("./cartpage.html");
    }
    function bindEvent(){
        addCart.add([changeSelfStyle,addCartsData,renderCartsNum]);
        reduceCart.add([reduceCartsData,renderCartsNum]);
        $(".addcart").on("click",addCart.fire);
        $(".reduceBtn").on("click",reduceCart.fire);
        $(".cartDiv").on("click",toCartPage);
    }
    //购物车
    $(init);
})(jQuery)